//
//  DLMachineTVHeaderView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/17.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMachineTVHeaderView.h"
#import "SDCycleBgView.h"
#import "DLMachineCycleModel.h"
#import "DLNewsFlashView.h"
#import "DLNewsFlashesListModel.h"

@interface DLMachineTVHeaderView()

@property (nonatomic, strong) SDCycleBgView *cycleBgView;

//@property (nonatomic, strong) DLNewsFlashView *newsFlashView;

@property (nonatomic, strong) NSMutableArray *newsFlashDataSource;



@end

@implementation DLMachineTVHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutSubControls];
        [self getCycleData];
        [self getNewsFlashData];
        self.backgroundColor = kColorRGB(250, 250, 250);
    }
    return self;
}
- (void) getNewsFlashData {
    
    NSString *uniqueId = [DLKeyChainManager readUUID];
    if (isEmpty(uniqueId)) {
        uniqueId = [DLKeyChainManager createUUID];
        [DLKeyChainManager saveUUID:uniqueId];
    }
    
    NSString *postUrl = HTTPNewInformation;
    NSDictionary *postDic = @{
                              @"user_id" : kuserManager.isLogin?kuserManager.userInfoModel.userid:uniqueId,
                              @"page" : @"1"
                              };
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        if (kRequestSuccess(responseObject)) {
            
            self.newsFlashDataSource = [NSMutableArray array];
            NSArray *dataSourceArr = responseObject[@"result"];
            if (dataSourceArr.count > 0) {
                
                for (NSDictionary *dataDic in dataSourceArr) {
                    DLNewsFlashesListModel *model = [DLNewsFlashesListModel mj_objectWithKeyValues:dataDic];
                    [self.newsFlashDataSource addObject:model];
                }
                
                self.newsFlashView.sourceArray = self.newsFlashDataSource;
            }
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            NSLog(@"首页短资讯接口未返回数据");
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"短资讯接口请求失败，错误是：%@", error);
//        kuserManager.lastVc.showNetworkErrorView = YES;
    }];
}
- (void) getCycleData {
    NSString *postUrl = HTTPHomeCycleScroll;
    NSDictionary *postDic = @{};
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        if (kRequestSuccess(responseObject)) {
            
            NSArray *resultArr = responseObject[@"result"];
            if ([resultArr isKindOfClass:[NSArray class]]) {
                NSMutableArray *resultMutArr = [NSMutableArray array];
                for (NSDictionary *dic in resultArr) {
                    DLMachineCycleModel *model = [DLMachineCycleModel mj_objectWithKeyValues:dic];
                    [resultMutArr addObject:model];
                }
                self.cycleBgView.cycleDataSource = resultMutArr;
            }
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            NSLog(@"首页轮播图接口未返回数据");
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
//        kuserManager.lastVc.showNetworkErrorView = YES;
    }];
}

- (void) layoutSubControls {
    
    SDCycleBgView *cycleBgView = [[SDCycleBgView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kAutoWidth(134))];
    [self addSubview:cycleBgView];
    self.cycleBgView = cycleBgView;
    
    DLNewsFlashView *newsFlashView = [[DLNewsFlashView alloc] initWithFrame:CGRectMake(cycleBgView.left, cycleBgView.bottom, cycleBgView.width, 40)];
    [self addSubview:newsFlashView];
    self.newsFlashView = newsFlashView;
    
    UIImageView *machineImgv = [UIImageView new];
    machineImgv.userInteractionEnabled = YES;
    machineImgv.image = [UIImage imageNamed:@"robot_bg"];
    machineImgv.frame = CGRectMake(10, newsFlashView.bottom, kScreenWidth - 20, kAutoWidth(380));
    [self addSubview:machineImgv];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(machineImgVTap)];
    [machineImgv addGestureRecognizer:tapGes];
    
    UIImageView *machineBtnImgV = [UIImageView new];
    machineBtnImgV.userInteractionEnabled = YES;
    machineBtnImgV.image = [UIImage imageNamed:@"robot_bgbtn"];
    machineBtnImgV.frame = CGRectMake((machineImgv.width - kAutoWidth(62)) / 2, kAutoWidth(276), kAutoWidth(62), kAutoWidth(60));
    [machineImgv addSubview:machineBtnImgV];
    UITapGestureRecognizer *tapGes1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(machineImgVTap)];
    [machineBtnImgV addGestureRecognizer:tapGes1];
    
    UILabel *textLabel = [UILabel new];
    textLabel.frame = CGRectMake(0, machineBtnImgV.bottom + 5, machineImgv.width, 30);
    textLabel.text = @"和小语聊聊吧";
    textLabel.font = kSystemFont(12);
    textLabel.textColor = kColorRGB(133, 142, 155);
    textLabel.textAlignment = NSTextAlignmentCenter;
    [machineImgv addSubview:textLabel];
    
    CGFloat moreHeight = kScreenSafeAreaHeight - kHeightTabBar - 64 - cycleBgView.height - newsFlashView.height - machineImgv.height;
    NSLog(@"差的像素是：%.2f", moreHeight);
    
    
    [machineBtnImgV resetY:kAutoWidth(276) + moreHeight / 4];
    [textLabel resetY:machineBtnImgV.bottom + 5];
    
    
    UIView *bottomView = [UIView new];
    bottomView.frame = CGRectMake(10, machineImgv.bottom, machineImgv.width, moreHeight);
    bottomView.backgroundColor = kColorRGB(223, 223, 223);
    [self addSubview:bottomView];
    [self sendSubviewToBack:bottomView];
    
    UITapGestureRecognizer *tapGes2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(machineImgVTap)];
    [bottomView addGestureRecognizer:tapGes2];
    
}

- (void) machineImgVTap {
    
    DLMachineDetailVC *machineDetailVC = [DLMachineDetailVC new];
    [kuserManager.lastVc.navigationController pushViewController:machineDetailVC animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
